var aFunc = {
	//项目的初始化
	readyApp: function() {
		//有值，说明已经显示过了，无需显示；
		var authToken = LocalStorage.getItem(LocalStorage.keys.Auth_Token);
		if (authToken != null && authToken.length > 0) {
			//关闭splash页面；
			plus.navigator.closeSplashscreen();
			plus.navigator.setFullscreen(false);
			plus.navigator.setStatusBarBackground(common.colour.statusBackground);
		} else {
			plus.navigator.setFullscreen(false);
			//预加载
			mui.openWindow({
				id: 'loginPage',
				url: 'views/sys/login/login.html',
				styles: {
					popGesture: "none"
				},
				show: {
					aniShow: 'slide-in-bottom'
				},
				waiting: {
					autoShow: false
				}
			});
		}
	},
	plusReady: function() {
		// 只有ios支持的功能需要在Android平台隐藏；
		if (mui.os.android) {
			var list = document.querySelectorAll('.ios-only');
			if (list) {
				for (var i = 0; i < list.length; i++) {
					list[i].style.display = 'none';
				}
			}
			//Android平台暂时使用slide-in-right动画
			if (parseFloat(mui.os.version) < 4.4) {
				aniShow = "slide-in-right";
			}
		}
		if (mui.os.plus) {
			plus.navigator.closeSplashscreen();
			plus.screen.lockOrientation("portrait-primary");
			plus.navigator.setStatusBarBackground(common.colour.statusBackground);
			aVariable.webview.current = plus.webview.currentWebview();
			aVariable.webview.current.setStyle({
				bounce: "vertical",
				bounceBackground: common.colour.pageBackground,
			});


			aFunc.readyApp();
		}
	
		aFunc.bindEvent();
		aFunc.initData();
		//页面内容跟随键盘弹起

		window.addEventListener('reloadHome', function(event) {
			aFunc.initData();
		});

		//从后台切换到前台
		document.addEventListener("resume", function() {
			aFunc.initData();
		}, false);
		
		aFunc.back();

	},


	initNativeObjects: function() {
		if (mui.os.android) {
			var main = plus.android.runtimeMainActivity();
			var Context = plus.android.importClass("android.content.Context");
			aVariable.InputMethodManager = plus.android.importClass(
				"android.view.inputmethod.InputMethodManager");
			aVariable.imm = main.getSystemService(Context.INPUT_METHOD_SERVICE);
		} else {
			aVariable.nativeWebview = plus.webview.currentWebview().nativeInstanceObject();
		}
	},
	showSoftInput: function() {
		if (mui.os.android) {
			aVariable.imm.toggleSoftInput(0, aVariable.InputMethodManager.SHOW_FORCED);
		} else {
			aVariable.nativeWebview.plusCallMethod({
				"setKeyboardDisplayRequiresUserAction": false
			});
		}
	},
	
	back: function() {
		mui.back = function(event) {
			aVariable.backButtonPress++;
			if (aVariable.backButtonPress > 1) {
				plus.runtime.quit();
			} else {
				plus.nativeUI.toast('再按一次退出应用');
			}
			setTimeout(function() {
				aVariable.backButtonPress = 0;
			}, 1000);
			return false;
		};
	},

	initData: function() {

		//用户信息
		attendServer.userdetails(function(data) {
			if (data.code == "200") {
				var orgName = "";
				var orgNames = data.data.organNames;
				if (orgNames.length >= 2) {
					orgName = " (" + orgNames[orgNames.length - 1] + ")";
				} else if (orgNames.length == 1) {
					orgName = " (" + orgNames[0] + ")";
				}
				aVariable.view.userName.innerHTML = data.data.userName + orgName;
				aVariable.view.phone.innerHTML = data.data.phone;
			} else {
				util.ToastTitle(data.msg);
			}
		}, function(error) {
			if (error != null) {
				if (JSON.stringify(error).length < 5) {
					util.ToastTitle("请求用户信息失败");
					return;
				}
			} else {
				util.ToastTitle("请求用户信息失败");
			}
		});

		//班次信息
		attendServer.shiftinfo(function(data) {
			if (data.code == "200") {
				if (data.data.length > 0) {
					var shiftData = data.data[data.data.length - 1];
					aVariable.data.clazzId = shiftData.clazzId;
					aVariable.view.onName.innerHTML = shiftData.clazzName;
					aVariable.view.onTime.innerHTML = "上班" + shiftData.onTime;
					if (shiftData.onStatus == 0) {
						aVariable.view.onStatus.innerHTML = "未签到";
						aVariable.view.onStatus.style.color = common.colour.fail;
						aVariable.btn.signIn.style.backgroundColor = common.colour.btnaAble;
						aVariable.btn.signIn.removeAttribute("disabled");
						aVariable.btn.signIn.innerHTML = "签到";
					} else if (shiftData.onStatus == 1) {
						aVariable.view.onStatus.innerHTML = "已签到";
						aVariable.view.onStatus.style.color = common.colour.sucess;
						aVariable.btn.signIn.style.backgroundColor = common.colour.btnNoable;
						aVariable.btn.signIn.setAttribute("disabled", true);
						aVariable.btn.signIn.innerHTML = shiftData.onSignTime;
					}

					aVariable.view.offName.innerHTML = shiftData.clazzName;
					aVariable.view.offTime.innerHTML = "下班" + shiftData.offTime;
					if (shiftData.offStatus == 0) {
						aVariable.view.offStatus.innerHTML = "未签退";
						aVariable.view.offStatus.style.color = common.colour.fail;
						aVariable.btn.signOut.style.backgroundColor = common.colour.btnaAble;
						aVariable.btn.signOut.removeAttribute("disabled");
						aVariable.btn.signOut.innerHTML = "签退";
					} else if (shiftData.offStatus == 1) {
						aVariable.view.offStatus.innerHTML = "已签退";
						aVariable.view.offStatus.style.color = common.colour.sucess;
						aVariable.btn.signOut.style.backgroundColor = common.colour.btnNoable;
						aVariable.btn.signOut.setAttribute("disabled", true);
						aVariable.btn.signOut.innerHTML = shiftData.offSignTime;
					}
				} else {
					util.ToastTitle("没有班次信息");
				}
			} else {
				util.ToastTitle(data.msg);
			}
		}, function(error) {
			if (error != null) {
				if (JSON.stringify(error).length < 5) {
					util.ToastTitle("请求班次失败");
					return;
				}
			} else {
				util.ToastTitle("请求班次失败");

			}
		});

		// attendServer.locations(function(data) {
		// 	if (data.code == "200") {

		// 	} else {
		// 		util.ToastTitle(data.msg);
		// 	}
		// }, function(error) {
		// 	if (error != null) {
		// 		util.ToastTitle(JSON.parse(error).msg);
		// 	} else {
		// 		util.ToastTitle("请求失败");
		// 	}
		// });


	},

	bindEvent: function() {

		aVariable.btn.signIn.addEventListener('tap', function(event) {
			aFunc.requestGeolocation(1);
		});

		aVariable.btn.signOut.addEventListener('tap', function(event) {
			aFunc.requestGeolocation(2);
		});

		mui('.look-over').on('tap', 'a', function(e) {
			mui.openWindow({
				id: "sign-details",
				url: "views/sign-details/sign-details.html",
				show: {
					aniShow: 'slide-in-right'
				},
				waiting: {
					autoShow: false
				}
			});
		});

		aVariable.btn.loginOut.addEventListener('tap', function() {
			var btnArray = ['否', '是'];
			mui.confirm('是否退出登录？', '提示', btnArray, function(e) {
				if (e.index == 1) {
					util.login_out();
					mui.openWindow({
						id: 'loginPage',
						url: 'views/sys/login/login.html',
						styles: {
							popGesture: "none"
						},
						show: {
							aniShow: 'none'
						},
						waiting: {
							autoShow: false
						}
					});
				} else {}
			});
		});

	},

	requestGeolocation: function(signType) {
		//获取设备的当前地理信息
		var wt = plus.nativeUI.showWaiting("处理中...");
		plus.geolocation.getCurrentPosition(function(position) {
			var latitude = position.coords.latitude;
			var longitude = position.coords.longitude;
			var poiName = position.address.poiName;
			var Precision = position.coords.accuracy;
			aFunc.requstaAttendee(latitude, longitude, signType, Precision, poiName, wt)
		}, function(e) {
			wt.close();
			alert(JSON.stringify(e))
			util.ToastTitle("获取异常:" + JSON.stringify(e));
		});

	},

	requstaAttendee: function(latitude, longitude, signType, Precision, poiName, wt) {

		var clazzId = aVariable.data.clazzId;
		if (clazzId == null || clazzId.length == 0) {
			wt.close();
			util.ToastTitle("没有班次唯一标识ID");
			return;
		}

		attendServer.attendee(latitude, longitude, signType, Precision, poiName, clazzId, function(data) {
			wt.close();
			if (data.code == "200") {
				aFunc.initData();
				if (signType == 1) {
					util.ToastTitle("签到成功");
				} else if (signType == 2) {
					util.ToastTitle("签退成功");
				} else {
					util.ToastTitle("其他情况成功");
				}
			} else {
				util.ToastTitle(data.msg);
			}
		}, function(error) {
			wt.close();
			if (error != null) {
				if (JSON.stringify(error).length < 5) {
					util.ToastTitle("请求打卡失败");
					return;
				}
				util.ToastTitle(JSON.parse(error).msg);
			} else {
				util.ToastTitle("请求打卡失败");
			}
		});
	}
};
